/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.ReceiveBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AdjustBillDao;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.PayModeDao;
import com.minierp.dao.ReceiveBillDao;
import com.minierp.dao.ReceiveBillItemDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.NotAllowException;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncReceiveBillFormCtrHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.CodeRule;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.Establishment;
import com.minierp.model.ReceiveBill;
import com.minierp.model.TallySet;
import com.minierp.service.ReceiveBillService;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class ReceiveBillFormCtr extends SimpleFormController {
	private DepartmentDao departmentDao;
	private BankAccountDao bankAccountDao;
	private ReceiveBillItemDao receiveBillItemDao;
	private ReceiveBillDao receiveBillDao;
	private AdjustBillDao adjustBillDao;
	private BusinessBillDao businessBillDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private BankAccountBalanceDao bankAccountBalanceDao;
	private PayModeDao payModeDao;
	private IdGenerator idGenerator;
	private ReceiveBillService receiveBillService;
	private DataSource dataSource;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		String retVal = request.getParameter("ret_val");
		if (retVal == null) {
			retVal = "";
		}

		refData.put("retVal", retVal);
		refData.put("departmentList", this.departmentDao.getList(true));
		List baList = this.bankAccountDao.getList("", "");
		EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		long dfTsId = 0L;
		long specId = 0L;
		if (id != null && !id.equals("")) {
			ReceiveBill bb = (ReceiveBill) this.receiveBillDao.get(
					ReceiveBill.class, Long.parseLong(id));
			specId = bb.getBankAccountId();
			dfTsId = bb.getTallySetId();
		} else {
			dfTsId = ub.getUser().getDfTsId();
		}

		refData.put("bankAccountList",
				PageHelper.parseBaList(baList, ent, ub, dfTsId, specId));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("RECEIVE_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for receive bill");
		} else {
			ReceiveBillBean form;
			if (id != null && !id.equals("")) {
				ReceiveBill arg18 = (ReceiveBill) this.receiveBillDao.get(
						ReceiveBill.class, Long.parseLong(id));
				if (arg18 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				Establishment arg19 = (Establishment) this.receiveBillDao.get(
						Establishment.class, arg18.getClientId());
				List arg21 = this.receiveBillItemDao.getListByReceiveBill(arg18
						.getId());
				TallySet arg20 = (TallySet) this.receiveBillDao.get(
						TallySet.class, arg18.getTallySetId());
				form = EncReceiveBillFormCtrHelper.doFormBackingObjectEdit(cr,
						arg18, arg19, arg20, arg21);
			} else {
				if (!EncCommonHelper.checkPrivilege(request, "RECE_BILL")) {
					throw new PermissionDennyException("Permission Denny");
				}

				String dataType = request.getParameter("data_type");
				Object bbList = new ArrayList();
				Establishment est = null;
				UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
				long tallySetId = ub.getUser().getDfTsId();
				if (dataType != null) {
					String clientId;
					if (!dataType.equals("BUSINESS")) {
						String arg22;
						String arg23;
						if (dataType.equals("CLIENT")) {
							clientId = request.getParameter("client_id");
							arg22 = request.getParameter("start_date");
							arg23 = request.getParameter("end_date");
							Date linkman = null;
							Date arg25 = null;
							SimpleDateFormat arg26 = new SimpleDateFormat(
									"yyyy-MM-dd");
							if (arg22 != null && !arg22.equals("")) {
								linkman = arg26.parse(arg22);
							}

							if (arg23 != null && !arg23.equals("")) {
								arg25 = arg26.parse(EncCommonHelper
										.getNextDateStr(arg23, "yyyy-MM-dd"));
							}

							est = (Establishment) this.receiveBillDao.get(
									Establishment.class,
									Long.parseLong(clientId));
							bbList = this.businessBillDao.getListForReceive(
									Long.parseLong(clientId), tallySetId,
									linkman, arg25);
						} else if (dataType.equals("ACCOUNT")) {
							clientId = request.getParameter("client_id");
							arg22 = request.getParameter("start_date");
							arg23 = request.getParameter("end_date");
							String arg24 = request.getParameter("linkman");
							String arg27 = request.getParameter("type");
							Date arg28 = null;
							Date arg29 = null;
							SimpleDateFormat sf = new SimpleDateFormat(
									"yyyy-MM-dd");
							if (arg22 != null && !arg22.equals("")) {
								arg28 = sf.parse(arg22);
							}

							if (arg23 != null && !arg23.equals("")) {
								arg29 = sf.parse(EncCommonHelper
										.getNextDateStr(arg23, "yyyy-MM-dd"));
							}

							bbList = this.businessBillDao.getListForAccountRec(
									Long.parseLong(clientId), arg28, arg29,
									arg24, tallySetId, arg27);
							est = (Establishment) this.receiveBillDao.get(
									Establishment.class,
									Long.parseLong(clientId));
						}
					} else {
						clientId = request.getParameter("business_bill_id_str");
						String[] startDateStr = clientId.split(";");
						long endDateStr = 0L;

						for (int type = 0; type < startDateStr.length; ++type) {
							BusinessBill startDate = (BusinessBill) this.businessBillDao
									.get(BusinessBill.class,
											Long.parseLong(startDateStr[type]));
							BusinessBillExt endDate = (BusinessBillExt) this.businessBillDao
									.get(BusinessBillExt.class,
											Long.parseLong(startDateStr[type]));
							if (startDate != null) {
								endDateStr = startDate.getClientId();
								((List) bbList).add(new Object[]{startDate,
										endDate});
							}
						}

						if (endDateStr != 0L) {
							est = (Establishment) this.receiveBillDao.get(
									Establishment.class, endDateStr);
						}
					}
				}

				form = EncReceiveBillFormCtrHelper.doFormBackingObjectNew(
						request, cr, est, (List) bbList);
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		ReceiveBillBean form = (ReceiveBillBean) command;
		EncReceiveBillFormCtrHelper.doOnBind(form, request, this.dataSource);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		ReceiveBillBean form = (ReceiveBillBean) command;
		ReceiveBill obj = form.getReceiveBill();
		if (examine != null && examine.equals("1")) {
			if (!EncCommonHelper.checkPrivilege(request, "RECE_EXAM")) {
				throw new Exception("Permission deny!");
			}

			obj.setExamined("Y");
			if (ub.getStaffer() != null) {
				obj.setExaminerId(ub.getStaffer().getId());
				obj.setExaminerName(ub.getStaffer().getStafferName());
			}
		} else {
			obj.setExamined("N");
		}

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("attach_file");
		String continueFlag;
		String printFlag;
		String printDetail;
		String alertType;
		if (multipartFile != null && !multipartFile.isEmpty()) {
			continueFlag = multipartFile.getOriginalFilename();
			printFlag = EncCommonHelper.getExtendName(continueFlag);
			printDetail = EncCommonHelper.getUploadVirtualPath(this
					.getServletContext());
			alertType = this.getServletContext().getRealPath(printDetail);
			String fileName = ub.getUser().getId() + "_"
					+ System.currentTimeMillis() + "." + printFlag;
			FileCopyUtils.copy(multipartFile.getBytes(), new File(alertType
					+ System.getProperty("file.separator") + fileName));
			obj.setAttachFile(printDetail + fileName);
		}

		if (id != null && !id.equals("")) {
			if (!EncCommonHelper.checkPrivilege(request, "RECE_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.receiveBillService.txUpdate(form, this.receiveBillDao,
						this.receiveBillItemDao, this.idGenerator,
						this.businessBillDao, this.adjustBillDao,
						this.bankAccountBalanceDao);
			} catch (CannotAcquireLockException arg17) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg18) {
				EncCommonHelper.setAlertMessage(request, arg18.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}
		} else {
			try {
				this.receiveBillService.txInsert(form, this.receiveBillDao,
						this.receiveBillItemDao, this.serialNumberDao,
						this.idGenerator, this.businessBillDao,
						this.adjustBillDao, this.bankAccountBalanceDao);
			} catch (CannotAcquireLockException arg19) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg20) {
				EncCommonHelper.setAlertMessage(request, arg20.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}
		}

		continueFlag = request.getParameter("continue_flag");
		printFlag = request.getParameter("print_flag");
		printDetail = request.getParameter("print_detail");
		alertType = request.getParameter("alert_type");
		if (continueFlag != null && continueFlag.equals("NEW")) {
			EncCommonHelper.setAlertMessage(request, "收款单保存成功！");
			mv = new ModelAndView(
					"redirect:/finance/ReceiveBillNew.ihtm?ret_val=1&alert_type="
							+ alertType);
		} else if (printFlag != null && printFlag.equals("SHOW")) {
			mv = new ModelAndView("redirect:/finance/ReceiveBillPrint.ihtm?id="
					+ obj.getId() + "&print_detail=" + printDetail);
		} else {
			if (alertType == null || !alertType.equals("NONE")) {
				EncCommonHelper.setAlertMessage(request, "收款单保存成功！");
			}

			if (printFlag != null && printFlag.equals("CALLBACK")) {
				mv.addObject("returnValue", String.valueOf(obj.getId()) + ";Y"
						+ ";" + printDetail);
			} else {
				mv.addObject("returnValue", String.valueOf(obj.getId()));
			}
		}

		mv.addObject("alertType", alertType);
		return mv;
	}

	public BusinessBillDao getBusinessBillDao() {
		return this.businessBillDao;
	}

	public void setBusinessBillDao(BusinessBillDao businessBillDao) {
		this.businessBillDao = businessBillDao;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public BankAccountDao getBankAccountDao() {
		return this.bankAccountDao;
	}

	public void setBankAccountDao(BankAccountDao bankAccountDao) {
		this.bankAccountDao = bankAccountDao;
	}

	public ReceiveBillDao getReceiveBillDao() {
		return this.receiveBillDao;
	}

	public void setReceiveBillDao(ReceiveBillDao receiveBillDao) {
		this.receiveBillDao = receiveBillDao;
	}

	public ReceiveBillItemDao getReceiveBillItemDao() {
		return this.receiveBillItemDao;
	}

	public void setReceiveBillItemDao(ReceiveBillItemDao receiveBillItemDao) {
		this.receiveBillItemDao = receiveBillItemDao;
	}

	public ReceiveBillService getReceiveBillService() {
		return this.receiveBillService;
	}

	public void setReceiveBillService(ReceiveBillService receiveBillService) {
		this.receiveBillService = receiveBillService;
	}

	public AdjustBillDao getAdjustBillDao() {
		return this.adjustBillDao;
	}

	public void setAdjustBillDao(AdjustBillDao adjustBillDao) {
		this.adjustBillDao = adjustBillDao;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public BankAccountBalanceDao getBankAccountBalanceDao() {
		return this.bankAccountBalanceDao;
	}

	public void setBankAccountBalanceDao(
			BankAccountBalanceDao bankAccountBalanceDao) {
		this.bankAccountBalanceDao = bankAccountBalanceDao;
	}

	public PayModeDao getPayModeDao() {
		return this.payModeDao;
	}

	public void setPayModeDao(PayModeDao payModeDao) {
		this.payModeDao = payModeDao;
	}
}